//
//  IGChangeiGalleryNameVC.m
//  jingdongfang
//
//  Created by BOE on 2017/8/23.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGChangeiGalleryNameVC.h"
#import "IGiGalleryChangeNameApi.h"

@interface IGChangeiGalleryNameVC ()

@property (nonatomic, strong) UITextField *nameTextField;
@property (nonatomic, strong) IGiGalleryChangeNameApi *api;
@property (nonatomic, strong) IGiGalleryEqu *model;

@end

@implementation IGChangeiGalleryNameVC
    
- (instancetype)initWithModel:(IGiGalleryEqu *)model {
    self = [super init];
    if (self) {
        self.model = model;
    }
    return self;
}
    
- (void)viewDidLoad {
    [super viewDidLoad];

//    self.title = @"修改设备名称";
    self.title = NSLocalizedString(@"Change Device Name", nil);

    self.view.backgroundColor = [UIColor mainBackgroundColor];
    
//    [self ig_setupRightButtonItemWithTitle:@"确认" image:nil highLightedImage:nil];
    [self ig_setupRightButtonItemWithTitle:NSLocalizedString(@"Confirm", nil) image:nil highLightedImage:nil];


    [self.view addSubview:self.nameTextField];
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(100);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(60);
    }];
}
    
- (UITextField *)nameTextField {
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc] init];
//        _nameTextField.placeholder = @"输入设备名字";
        _nameTextField.placeholder = NSLocalizedString(@"iGallery Name", nil);

        UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
        _nameTextField.leftView = leftView;
        _nameTextField.leftViewMode = UITextFieldViewModeAlways;
        _nameTextField.backgroundColor = [UIColor whiteColor];
        _nameTextField.text = self.model.title;
        [_nameTextField addTarget:self action:@selector(nameTextFiledChangeValueAction:) forControlEvents:UIControlEventEditingChanged];
    }
    return _nameTextField;
}

- (void)ig_rightButtonPressed:(id)sender {
    
    [_nameTextField endEditing:YES];
    
    if (_nameTextField.text.length == 0) {
//        [self presentMessageTips:@"请输入设备名称"];
        [self presentMessageTips:NSLocalizedString(@"Please Input Your Device Name", nil)];
        return;
    }
    if ([_nameTextField.text isEqualToString:self.model.title]) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }

    IGiGalleryChangeNameApi *api = [[IGiGalleryChangeNameApi alloc] initWithMacId:self.model.macId title:_nameTextField.text type:self.model.type];
    [api startWithDecodeClass:[IGiGalleryChangeNameResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            
            self.model.title = _nameTextField.text;
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

- (void)nameTextFiledChangeValueAction:(UITextField *)textField{
    
    UITextRange *markedRange = [textField markedTextRange];
    if (markedRange) {
        return;
    }
    
    if (textField.text.length > 12) {
//        [self presentMessageTips:@"最多可输入12个字"];
        [self presentMessageTips:NSLocalizedString(@"Input no more than 12 characters.", nil)];

        NSRange range = [textField.text rangeOfComposedCharacterSequenceAtIndex:12];
        textField.text = [textField.text substringToIndex:range.location];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_nameTextField becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_nameTextField endEditing:YES];
}

@end
